home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 937 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  83 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: boukanov@sentef1.fi.uib.no (Igor Boukanov)
  3. Newsgroups: comp.std.c++
  4. Subject: ?s about exceptions in destructors
  5. Date: 2 Apr 1996 15:16:17 GMT
  6. Organization: Fysisk institutt, Universitetet i Bergen
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4jrf96$1mj@ugress.uib.no>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. Keywords: C++, destructor, exception
  11. X-Nntp-Posting-Host: sentef1.fi.uib.no
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. Content-Length: 1323
  14. Originator: clamage@taumet
  15.  
  16. Consider the following:
  17. struct A { 
  18.    ~A(){throw int(0);} 
  19. };
  20.  
  21. struct B {
  22.    A a;
  23.    ~B(){throw float(0.0);}
  24. };
  25.  
  26. void f() { B b; }
  27.  
  28. So according to the 09.95 DWP, 15.5.1.1 any call to f will call 
  29. terminate().
  30.  
  31. But what should happen in the next code:
  32.  
  33. void f2() {
  34.    char buf[sizeof(B)];
  35.    B* b = new(buf) B;
  36.    b->B::~B();
  37. };
  38.  
  39. void g2() {
  40.    try { f2(); } catch(float) { }
  41. }
  42.  
  43. I suppose that g2 will catch float exception after "throw float(0.0);" 
  44. in B::~B and A::~A of b->a will not be called because according to 15.2:
  45. " 1 As  control  passes  from  a throw-point to a handler, destructors are
  46.   invoked for all automatic objects constructed since the try block  was
  47.   entered.
  48. 2 An object that is partially constructed will have destructors executed
  49.   only for its fully constructed sub-objects..."
  50.  
  51. and because b is NOT a automatic object so b->a is NOT a automatic object too.
  52.  
  53. Is this right?
  54.  
  55. And what should happen in the next:
  56. void f3() {
  57.    B* b = new B;
  58.    delete b;
  59. };
  60.  
  61. Is it supposed that this lines can be rewritten as:
  62. void f4() {
  63.    B* b = new B;
  64.    try { b->B::~B(); }
  65.    catch(...) { operator delete(b); throw; }
  66.    operator delete(b);
  67. };
  68.  
  69. So A::~A() for b->a will not be called again but memory will be released?
  70.  
  71.  
  72. --
  73. Regards, Igor Boukanov. 
  74. igor.boukanov@fi.uib.no  
  75. http://www.fi.uib.no/~boukanov/
  76.  
  77.  
  78. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  79. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  80. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  81. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  82. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  83.